Creating a Luks encrypted backup disk
Written: | 2025-04-07 |
Tags: | #how-to #snippet |
Here's a small fish script for automatically creating LUKS encrypted harddrives that store their key in pass
.
set -l dev /dev/sdf
set -l part "$dev"1
set -l name epsilon
# format disk, create partition
echo -e "g\nn\n\n\n\n\nw" | sudo fdisk $dev
# create key:
head -c 128 /dev/random | base64 -w 0 | pass insert -m devices/$name
# create luks container
pass devices/$name | head -n 1 | sudo cryptsetup luksFormat $part -
# open luks container
pass devices/$name | head -n 1 | sudo cryptsetup luksOpen $part $name -
# format with exfat, label partition $name
sudo mkfs.exfat -n $name /dev/mapper/$name
# write mounts line
# grab uuid first (do it twice, first read reads a stale value somehow)
set -l uuid (lsblk -no UUID $part | head -n 1)
set uuid (lsblk -no UUID $part | head -n 1)
while [ ! (realpath /dev/disk/by-uuid/$uuid) = $part ]
read -l -P "Read wrong uuid, retry? [Y/n] " choice
if [ -n "$choice" -a "$choice" != "y" -a "$choice" != "Y" ]
return 1
end
set uuid (lsblk -no UUID $part | head -n 1)
end
echo "found uuid of disk $part to be $uuid."
# check if mounts file even exists
if [ -f ~/.mounts ]
echo -e "/dev/disk/by-uuid/$uuid\t/run/media/$USER/$name\tmount-luks \$device 'devices/$name' $name\tmount-luks -u '$name'\t$name" >> ~/.mounts
echo "wrote line to .mounts file!"
end
Works great in coordination with my mnt-fish function for mounting/unmounting drives.